package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrComConfigSortBinding;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.sgb.kjwl.model.entity.home.RefreshHomeNoticeEVent;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.manage.EnterpriseAndMapActivity;
import com.sgb.kjwl.view.ui.adapter.home.DragCompanyConfigAdapter;
import com.sgb.kjwl.viewmodel.home.HomeCompanyConfigModel;
import com.sgb.member.ui.activity.MemberInviteOrApplyActivity;
import com.swgk.core.customview.ItemDragHelperCallback;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 重新排序界面
 * LiWei 2021/10/29
 */
@Deprecated
public class CompanyConfigSortFragment extends AppLazyFragment {

    private FrComConfigSortBinding binding;
    private HomeCompanyConfigModel viewModel;

    private DragCompanyConfigAdapter configAdapter;

    public static CompanyConfigSortFragment newInstance() {
        CompanyConfigSortFragment dialog = new CompanyConfigSortFragment();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_com_config_sort, container, false);
        viewModel = new ViewModelProvider(getActivity()).get(HomeCompanyConfigModel.class);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        viewModel.liveData6.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
               if (list.size() == 0){
                   binding.rlvMenu.setVisibility(View.GONE);
                   binding.tvDefaultHint.setVisibility(View.VISIBLE);
               }else {
                   binding.rlvMenu.setVisibility(View.VISIBLE);
                   binding.tvDefaultHint.setVisibility(View.GONE);
                   initHomeMenu(list);
               }
                //通知大首页刷新数据
                EventBus.getDefault().postSticky(new RefreshHomeNoticeEVent(EventConstant.EVENT_NOTICE_REFRESH_HOME));
            }
        });
        viewModel.liveData7.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0){
                    binding.tvMember1DefaultHint.setVisibility(View.VISIBLE);
                }else {
                    binding.tvMember1DefaultHint.setVisibility(View.GONE);
                    initMember1Menu(list);
                }
            }
        });
        viewModel.liveData8.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0){
                    binding.linMemberDefault.setVisibility(View.VISIBLE);
                    binding.btnMemberInvite.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MemberInviteOrApplyActivity.startInviteOrApply(getActivity(), 0);
                        }
                    });
                }else {
                    binding.linMemberDefault.setVisibility(View.GONE);
                    initMemberMenu(list);
                }
            }
        });
        viewModel.liveData9.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0){
                    binding.linAttentionDefault.setVisibility(View.VISIBLE);
                    binding.btnAttentionInvite.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
                            String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
                            EnterpriseAndMapActivity.start(getActivity(), longitude, latitude, false);
                        }
                    });
                }else {
                    binding.linAttentionDefault.setVisibility(View.GONE);
                    initAttentionMenu(list);
                }
            }
        });
        viewModel.liveData10.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                initRecommendMenu(list);
            }
        });
        viewModel.liveData11.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                viewModel.editHomeCompanyMenu(list);
            }
        });
    }

    private void initHomeMenu(List<EnterpriseRecEntity> list) {
        if (list.size() > 0) {
            if (configAdapter == null) {
                GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
                binding.rlvMenu.setLayoutManager(manager);
                binding.rlvMenu.addItemDecoration(new RecyclerCustomDecoration(
                        getResources().getDimension(R.dimen.dp8),
                        0,
                        getResources().getDimension(R.dimen.dp8),
                        0));
                ItemDragHelperCallback callback = new ItemDragHelperCallback();
                final ItemTouchHelper helper = new ItemTouchHelper(callback);
                helper.attachToRecyclerView(binding.rlvMenu);
                configAdapter = new DragCompanyConfigAdapter(getActivity(), helper, list);
                configAdapter.setListener(new DragCompanyConfigAdapter.OnClickItemListener() {

                    @Override
                    public void onDelete(List<EnterpriseRecEntity> editList) {
                        viewModel.editHomeCompanyMenu(editList);
                    }

                    @Override
                    public void onFinish(List<EnterpriseRecEntity> editList) {

                    }
                });
                manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        int viewType = configAdapter.getItemViewType(position);
                        return viewType == DragCompanyConfigAdapter.TYPE_MY ? 1 : 3;
                    }
                });
                binding.rlvMenu.setAdapter(configAdapter);
            } else {
                configAdapter.setData(list);
            }
        }
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getCompanyConfigList("1", false);
        viewModel.getCompanyConfigList("3", false);
        viewModel.getCompanyConfigList("4", false);
        viewModel.getCompanyConfigList("5", false);

        int TYPE = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (TYPE == 0) {
            binding.linMember1.setVisibility(View.GONE);
            viewModel.getCompanyConfigList("3", false);
        }
        if (TYPE == 1) {
            viewModel.getCompanyConfigList("2", false);
        }
        if (TYPE == 2 || TYPE == 3) {
            binding.linMember1.setVisibility(View.GONE);
            binding.linMember.setVisibility(View.GONE);
        }
    }

    public int COLUMN_COUNT = 3;
    public int ROW_COUNT = 3;
    private ImageView[] member1MenuTip;
    private int member1Page = 1;
    private ImageView[] memberMenuTip;
    private int memberPage = 1;
    private ImageView[] attentionMenuTip;
    private int attentionPage = 1;
    private ImageView[] recMenuTip;
    private int recPage = 1;

    private void initMember1Menu(List<EnterpriseRecEntity> list) {
        ArrayList<Fragment> member1MenuFragment = new ArrayList<>();
        int total = list.size();
        member1Page = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < member1Page; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            member1MenuFragment.add(EnterpriseConfigMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT, i));
            EnterpriseConfigMenuFragment big = (EnterpriseConfigMenuFragment) member1MenuFragment.get(i);
            big.setOnItemClickCallBack(new EnterpriseConfigMenuFragment.OnItemClickCallBack() {

                @Override
                public void onClick(EnterpriseRecEntity entity) {
                    viewModel.getCompanyList(entity);
                }
            });
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), member1MenuFragment);
        binding.consMember1ViewPage.setAdapter(menuAdapter);
        binding.consMember1ViewPage.setOffscreenPageLimit(member1Page);
        binding.consMember1ViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setMember1ImageBackgroundMenu(position % member1Page);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (member1Page > 1) {
            binding.dotMember1MenuLayout.setVisibility(View.VISIBLE);
            binding.dotMember1MenuLayout.removeAllViews();
            initMember1DotsMenu(0);
        } else {
            binding.dotMember1MenuLayout.removeAllViews();
            binding.dotMember1MenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initMember1DotsMenu(int selectItems) {
        member1MenuTip = new ImageView[member1Page];
        for (int i = 0; i < member1Page; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            member1MenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                member1MenuTip[i].setSelected(true);
            } else {
                member1MenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.dotMember1MenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setMember1ImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < member1MenuTip.length; i++) {
            if (i == selectItems) {
                member1MenuTip[i].setSelected(true);
            } else {
                member1MenuTip[i].setSelected(false);
            }
        }
    }

    private void initMemberMenu(List<EnterpriseRecEntity> list) {
        ArrayList<Fragment> memberMenuFragment = new ArrayList<>();
        int total = list.size();
        memberPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < memberPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            memberMenuFragment.add(EnterpriseConfigMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT, i));
            EnterpriseConfigMenuFragment big = (EnterpriseConfigMenuFragment) memberMenuFragment.get(i);
            big.setOnItemClickCallBack(new EnterpriseConfigMenuFragment.OnItemClickCallBack() {

                @Override
                public void onClick(EnterpriseRecEntity entity) {
                    viewModel.getCompanyList(entity);
                }
            });
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), memberMenuFragment);
        binding.consMemberViewPage.setAdapter(menuAdapter);
        binding.consMemberViewPage.setOffscreenPageLimit(memberPage);
        binding.consMemberViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setMemberImageBackgroundMenu(position % memberPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (memberPage > 1) {
            binding.dotMemberMenuLayout.setVisibility(View.VISIBLE);
            binding.dotMemberMenuLayout.removeAllViews();
            initMemberDotsMenu(0);
        } else {
            binding.dotMemberMenuLayout.removeAllViews();
            binding.dotMemberMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initMemberDotsMenu(int selectItems) {
        memberMenuTip = new ImageView[memberPage];
        for (int i = 0; i < memberPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            memberMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                memberMenuTip[i].setSelected(true);
            } else {
                memberMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.dotMemberMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setMemberImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < memberMenuTip.length; i++) {
            if (i == selectItems) {
                memberMenuTip[i].setSelected(true);
            } else {
                memberMenuTip[i].setSelected(false);
            }
        }
    }

    private void initAttentionMenu(List<EnterpriseRecEntity> list) {
        ArrayList<Fragment> attentionMenuFragment = new ArrayList<>();
        int total = list.size();
        attentionPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < attentionPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            attentionMenuFragment.add(EnterpriseConfigMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT, i));
            EnterpriseConfigMenuFragment big = (EnterpriseConfigMenuFragment) attentionMenuFragment.get(i);
            big.setOnItemClickCallBack(new EnterpriseConfigMenuFragment.OnItemClickCallBack() {

                @Override
                public void onClick(EnterpriseRecEntity entity) {
                    viewModel.getCompanyList(entity);
                }
            });
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), attentionMenuFragment);
        binding.attentionConsViewPage.setAdapter(menuAdapter);
        binding.attentionConsViewPage.setOffscreenPageLimit(attentionPage);
        binding.attentionConsViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setAttentionImageBackgroundMenu(position % attentionPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (attentionPage > 1) {
            binding.attentionDotMenuLayout.setVisibility(View.VISIBLE);
            binding.attentionDotMenuLayout.removeAllViews();
            initAttentionDotsMenu(0);
        } else {
            binding.attentionDotMenuLayout.removeAllViews();
            binding.attentionDotMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initAttentionDotsMenu(int selectItems) {
        attentionMenuTip = new ImageView[attentionPage];
        for (int i = 0; i < attentionPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            attentionMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                attentionMenuTip[i].setSelected(true);
            } else {
                attentionMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.attentionDotMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setAttentionImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < attentionMenuTip.length; i++) {
            if (i == selectItems) {
                attentionMenuTip[i].setSelected(true);
            } else {
                attentionMenuTip[i].setSelected(false);
            }
        }
    }

    private void initRecommendMenu(List<EnterpriseRecEntity> list) {
        ArrayList<Fragment> recMenuFragment = new ArrayList<>();
        int total = list.size();
        recPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < recPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            recMenuFragment.add(EnterpriseConfigMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT, i));
            EnterpriseConfigMenuFragment big = (EnterpriseConfigMenuFragment) recMenuFragment.get(i);
            big.setOnItemClickCallBack(new EnterpriseConfigMenuFragment.OnItemClickCallBack() {

                @Override
                public void onClick(EnterpriseRecEntity entity) {
                    viewModel.getCompanyList(entity);
                }
            });
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), recMenuFragment);
        binding.recConsViewPage.setAdapter(menuAdapter);
        binding.recConsViewPage.setOffscreenPageLimit(recPage);
        binding.recConsViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setRecImageBackgroundMenu(position % recPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (recPage > 1) {
            binding.recDotMenuLayout.setVisibility(View.VISIBLE);
            binding.recDotMenuLayout.removeAllViews();
            initRecDotsMenu(0);
        } else {
            binding.recDotMenuLayout.removeAllViews();
            binding.recDotMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initRecDotsMenu(int selectItems) {
        recMenuTip = new ImageView[recPage];
        for (int i = 0; i < recPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            recMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                recMenuTip[i].setSelected(true);
            } else {
                recMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.recDotMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setRecImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < recMenuTip.length; i++) {
            if (i == selectItems) {
                recMenuTip[i].setSelected(true);
            } else {
                recMenuTip[i].setSelected(false);
            }
        }
    }
}
